package com.mytx.notify.redisMessageQueue;

import com.mytx.common.constant.Constant;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.ArrayList;
import java.util.List;

@Configuration
@AutoConfigureAfter({Receiver.class})
public class SubscriberConfig {

    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver);
    }


    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        List<PatternTopic> patternTopics = new ArrayList<>();
        //user 服务相关的推送
        patternTopics.add(new PatternTopic(Constant.NOTIFY_ADVERTISEMENT_NEED_SET_SHOW_CONTENT));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_OFFER_FAIL));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_PERSON_RECEIVED_DONATION));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_PERSON_RECEIVED_FORWARD));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_SYNC_USER_INFO));

        //live服务相关的推送
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_TO_START));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_TO_STOP));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_RECEIVED_REWARD));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_RECEIVED_MULCT));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_RECEIVED_OFFER));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_LIVE_RECEIVED_DONATION));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_ADVERTISEMENT_CHANGE));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_CHATID_CHANGE_INFO));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_MODIFY_ADVERTISEMENT_MAX_PRICE));
        //search 服务相关的推送
        patternTopics.add(new PatternTopic(Constant.NOTIFY_NEW_TOPIC));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_FOUND_COUPON));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_OFFER_TO_TOPIC_SUCCESS));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_ORDER_INFO));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_TASK_INFO));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_TOPIC_VERIFY));
        //search 服务需要推送消息走的消息队列，没有用feign调用服务的方式
        patternTopics.add(new PatternTopic(Constant.NOTIFY_PUSH_MESSAGE));
        patternTopics.add(new PatternTopic(Constant.NOTIFY_CHAR_ROOM_MESSAGE));

        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, patternTopics);
        return redisMessageListenerContainer;
    }


}
